export default {
  namespaced: true,
  state: {
    // 用户信息
    info: {}
  },

  mutations: {

    /**
     * 设置角色
     */
    SET_HAVE_ROLE(state, haveRole) {
      state.haveRole = haveRole
      // 持久化
      this.dispatch('d2admin/db/set', {
        dbName: 'sys',
        path: 'user.haveRole',
        value: haveRole,
        user: true
      })
    },
    /**
     * 设置access_token
     */
    SET_ACCESS_TOKEN(state, accessToken) {
      state.accessToken = accessToken
      // 持久化
      this.dispatch('d2admin/db/set', {
        dbName: 'sys',
        path: 'user.accessToken',
        value: accessToken,
        user: true
      })
    },

    /**
     * 设置refresh_token
     */
    SET_REFRESH_TOKEN(state, refreshToken) {
      state.refreshToken = refreshToken
      // 持久化
      this.dispatch('d2admin/db/set', {
      dbName: 'sys',
      path: 'user.refreshToken',
      value: refreshToken,
      user: true
      })
    },

    
  },

  actions: {
    /**
     * @description 设置用户数据
     * @param {Object} context
     * @param {*} info info
     */
    async set ({ state, dispatch }, info) {
      // store 赋值
      state.info = info
      // 持久化
      await dispatch('d2admin/db/set', {
        dbName: 'sys',
        path: 'user.info',
        value: info,
        user: true
      }, { root: true })
    },
    /**
     * @description 从数据库取用户数据
     * @param {Object} context
     */
    async load ({ state, dispatch }) {
      // store 赋值
      state.info = await dispatch('d2admin/db/get', {
        dbName: 'sys',
        path: 'user.info',
        defaultValue: {},
        user: true
      }, { root: true })
    }
  }
}
